package com.lagou.edu.test;

import com.lagou.edu.annotation.Autowired;
import com.lagou.edu.annotation.Component;
import com.lagou.edu.annotation.Repository;
import com.lagou.edu.annotation.Service;
import com.lagou.edu.dao.impl.JdbcAccountDaoImpl;
import com.lagou.edu.factory.BeanFactory;
import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class test {
    @Test
    public void test() throws ClassNotFoundException {
        Class<?> aClass = Class.forName("com.lagou.edu.dao.impl.JdbcAccountDaoImpl");
        Annotation[] annotations = aClass.getAnnotations();
        boolean isAdd = false;
        for (Annotation annotation : annotations) {
            if (annotation instanceof Component||annotation instanceof Repository||annotation instanceof Service){
                isAdd = true;
            }
        }
        if(isAdd){
            String simpleName = lowerFirstChar(aClass.getSimpleName());
            System.out.println("simpleName="+simpleName);
            Class<?>[] interfaces = aClass.getInterfaces();
            if (interfaces.length>0){
                for (Class<?> anInterface : interfaces) {
                    String interfaceName = anInterface.getName();
                    System.out.println("interfaceName="+interfaceName);
                }
            }

        }

    }

    @Test
    public void test2() throws ClassNotFoundException {
        Class<?> aClass = Class.forName("com.lagou.edu.dao.impl.JdbcAccountDaoImpl");
        Annotation[] annotations = aClass.getAnnotations();
        boolean isAdd = false;
        for (Annotation annotation : annotations) {
            if (annotation instanceof Component||annotation instanceof Repository||annotation instanceof Service){
                isAdd = true;
            }
        }
        if(isAdd){
            Field[] declaredFields = aClass.getDeclaredFields();
            if (declaredFields.length>0){
                for (Field declaredField : declaredFields) {
                    Annotation[] fieldAnnotations = declaredField.getDeclaredAnnotations();
                    for (Annotation fieldAnnotation : fieldAnnotations) {
                        if(fieldAnnotation instanceof Autowired){
                            String name = declaredField.getType().getName();
                            System.out.println("name="+name);
                        }
                    }
                }
            }

        }

    }


    @Test
    public void test3() throws ClassNotFoundException {
        Map<String,Object> map = new HashMap<>();
        TestBean testBean = new TestBean();
        map.put("bean1",testBean);
        map.put("com.lagou.edu.test.TestBean",testBean);
        TestBean2 testBean2 = new TestBean2();
        map.put("bean2",testBean2);
        map.put("com.lagou.edu.test.TestBean2",testBean2);
        map.put("bean3",new TestBean3());

        TestBean bean1 = (TestBean)map.get("bean1");
        bean1.setTestBean2((TestBean2)map.get("bean2"));
        TestBean2 bean2 = (TestBean2)map.get("bean2");
        bean2.setTestBean3((TestBean3)map.get("bean3"));

        TestBean bean1_1 = (TestBean)map.get("bean1");
        bean1_1 = new TestBean();

        System.out.println("sss");


    }

    @Test
    public void test4() throws ClassNotFoundException {
        Object jdbcAccountDaoImpl = BeanFactory.getBean("jdbcAccountDaoImpl");
        System.out.println(jdbcAccountDaoImpl);

    }

    @Test
    public void test5() throws ClassNotFoundException {

    }



    private String lowerFirstChar(String str) {
        char[] chars = str.toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }

}
